/**
 * author: shanzhitong
 */
var form, layer, laypage, table, layedit, laydate, flow, update;
define(['common'], function(common) {
	return {
		init: function() {
			this.initLayui();
		},
		initLayui: function() {
			var that_ = this;
			layui.config({
				base: ctx + '/plugins/',
	            v: new Date().getTime()
			}).use(['layer', 'laypage', 'table', 'form', 'layedit', 'laydate', 'flow', 'upload'], function(){
				var $ = layui.jquery;
					form = layui.form;
					layer = layui.layer;
					laypage = layui.laypage;
					table = layui.table;
					layedit = layui.layedit;
					laydate = layui.laydate;
					flow = layui.flow;
					update = layui.upload;
				//全选
				form.on('checkbox(allChoose)', function(data){
					var chooseName = $(data.elem).attr("name").replace(/all/, "").replace(/(\w)/,function(v){return v.toLowerCase()});
					var child = $('tbody input[type="checkbox"][name="' + chooseName + '"]');
					child.each(function(index, item) {
						if (typeof $(item).attr("disabled") == 'undefined') {
							item.checked = data.elem.checked;
						}
					});
					form.render('checkbox');
				});
				//绑定省市联动selelct change事件
			    form.on('select(changeRegions)', function(ele) {
			    	var obj = document.addForm[$(ele.elem).closest("div").next().find("select").attr("name")];
			    	common.changeRegion(obj, ele.value);
			    	form.render();
				});
				$.each($("input[layuidate='true']"), function() {
					laydate.render({
						elem: this,
						trigger: 'click'
					});
				})
				$.each($("input[layuidatetime='true']"), function() {
					laydate.render({
						elem: this,
						trigger: 'click',
						type: 'datetime'
					});
				})
				//消息通知
				if (typeof $("#div-message") != 'undefined' && $("#div-message").length > 0 && $("#div-message").text() != "") {
					layer.msg($("#div-message").text());
				}
				//初始化table高度和td宽度
				that_.initTable();
				if (typeof page != 'undefined') {
					//调用页面分页方法
					page();
				}
				if (typeof ready != 'undefined') {
					//调用页面ready()方法
					ready();
				}
			});
		},
		initTable: function() {
			var $th = $(".data-grid-group").find("table.dymanic-table").find("th");
			var pageHelperLength = $(".page-helper").length;
			//转换静态限制每页条数
			table.set({
				limit: 100000
			});
			//转换静态表格
			table.init('data-grid', {
			    height: document.body.clientHeight - (pageHelperLength == 0 ? 136 : 186)//设置高度
			});
			if (pageHelperLength == 0) {
				$(".layui-table-view").css("margin", "0");
			}
			this.hideTableScroll();
			this.overwriteSelectAll();
		},
		overwriteSelectAll : function() {
			//重写全选按钮方法
			table.on('checkbox(data-grid)', function(obj) {
				if (obj.type == 'all') {//全选
					$.each($(".dymanic-table").find("tbody").find("input[type='checkbox']"), function(i, o) {
						if (obj.checked) {
							$(o).prop("checked", obj.checked);
						} else {
							$(o).removeAttr("checked");
						}
					});
				} else {//单选
					var checkboxName = $(".dymanic-table").find("tbody").find("input[type='checkbox']").eq(0).attr("name");
					var chooseValue = obj.data[checkboxName];
					chooseValue = chooseValue.substring(chooseValue.indexOf("value=") + 7);
					chooseValue = chooseValue.substring(0, chooseValue.indexOf('"'));
					var $obj = $("input[type='checkbox'][name='" + checkboxName + "'][value='" + chooseValue + "']");
					if ($obj && obj.checked) {
						$obj.prop("checked", obj.checked);
					} else {
						$obj.removeAttr("checked");
					}
				}
			});
		},
		hideTableScroll : function() {
			//隐藏table滚动条方法
			var $tableBody = $(".layui-table-body");
			$tableBody.scrollTop(1);
			if ($tableBody.scrollTop() > 0) {//有滚动条
				$tableBody.css("width", "calc(100% + 17px)");
			}
		}
	}
});